package com.jincheng.web.controller.common;

import com.jincheng.common.constant.CacheConstants;
import com.jincheng.common.constant.Constants;
import com.jincheng.common.core.domain.AjaxResult;
import com.jincheng.common.core.redis.RedisCache;
import com.jincheng.common.utils.uuid.IdUtils;
import com.jincheng.system.service.IQrCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 二维码
 */
@RestController
public class QrCodeController {

    @Autowired
    private IQrCodeService qrCodeService;

    @Autowired
    private RedisCache redisCache;

    @GetMapping("/getQrCode")
    public AjaxResult getQrCode(@RequestParam String content) {
        String uuid = IdUtils.simpleUUID();
        String verifyKey = CacheConstants.QR_CODE_KEY + uuid;
        redisCache.setCacheObject(verifyKey, content, Constants.QR_EXPIRATION, TimeUnit.SECONDS);
        AjaxResult ajax = AjaxResult.success();
        String image = qrCodeService.createCodeToStream(uuid);
        ajax.put("img", image);
        return ajax;
    }
}
